﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Universum.Controllers;
using Universum.Infrastructure.Persistence;
using Universum.Model.Domain;
using Universum.Infrastructure.Persistence.Queries;
using Universum;
using Universum.Infrastructure.Configuration;
namespace Universum.Infrastructure.Security
{
	public class UserMembershipService : IMembershipService
	{
		IRepository _repository;
		public UserMembershipService(IRepository repository)
		{
			_repository = repository;
		}
		#region IMembershipService Members

		public int MinPasswordLength
		{
			get { return 1; }
		}

		public bool ValidateUser(string userName, string password)
		{
			var noOfUsers = _repository.Query<User>().Count();
			if (noOfUsers == 0)
			{
				CreateUser(ConstantIdentifiers.SPECIAL_USER,
					"asdf1234qwer", 					
					ConstantIdentifiers.SPECIAL_USER+"@localhost.lo");
			}

			return _repository.Query(new UserWithUsernameAndPassword(userName, password)).SingleOrDefault() != null;
		}

		public System.Web.Security.MembershipCreateStatus CreateUser(string userName, string password, string email)
		{
			User user = new User
			{
				Username = userName,
				Password = password,
				ID = Guid.NewGuid(),
				Email = email
			};

			if (email_is_already_registered(email)) return System.Web.Security.MembershipCreateStatus.DuplicateEmail;
			if (email_is_not_valid(email)) return System.Web.Security.MembershipCreateStatus.InvalidEmail;
			if (username_is_not_valid(userName)) return System.Web.Security.MembershipCreateStatus.InvalidUserName;
			if (username_is_already_registered(userName)) return System.Web.Security.MembershipCreateStatus.DuplicateUserName;
			
			_repository.SaveOrUpdate(user);
			return System.Web.Security.MembershipCreateStatus.Success;
		}

		private bool username_is_not_valid(string userName)
		{
			if (userName.IsNullOrEmpty()) return true;
			if (false == userName.IsValidUsername()) return true;
			return false;
		}

		private bool email_is_not_valid(string email)
		{
			if (email.IsNullOrEmpty()) return true;
			if (false == email.IsValidEmail()) return true;
			return false;
		}

		private bool username_is_already_registered(string userName)
		{
			if (userName.IsNullOrEmpty()) return false;
			return _repository.Query(new UserWithUsername(userName)).SingleOrDefault() != null;
		}

		private bool email_is_already_registered(string email)
		{
			if (email.IsNullOrEmpty()) return false;
			return _repository.Query(new UserWithEmail(email)).SingleOrDefault() != null;
		}

		public bool ChangePassword(string userName, string oldPassword, string newPassword)
		{
			var user = _repository.Query(new UserWithUsername(userName)).SingleOrDefault();
			if (user == null) return false;
			if (user.Password.Equals(oldPassword).IsFalse()) return false;
			user.Password = newPassword;
			_repository.SaveOrUpdate(user);
			return true;
		}

		#endregion
	}
}
